/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.google.blockly.ohos.ui;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

/**
 * ViewGroup that can rotate a child view by 90&ordm; either clockwise or counter-clockwise.
 * While not fully supported, if the view has multiple children, they will all be rotated in the
 * same direction, just within the {@code RotatedViewGroup's} padding.
 */
public class RotatedViewGroup extends ComponentContainer implements Component.EstimateSizeListener, ComponentContainer.ArrangeListener {
    private @Rotation.Enum
    int mRotation = Rotation.NONE;

    private boolean mRotationChanged = true;
    private final Matrix mEventTransformMatrix = new Matrix();
    private final Matrix mDrawTransformMatrix = new Matrix();  // Inverse of Event transform.
    private final Rect mChildLayoutRect = new Rect();
    private final float[] mViewTouchPoint = new float[2];
    private final float[] mChildTouchPoint = new float[2];

    public RotatedViewGroup(Context context) {
        this(context, null);
    }

    public RotatedViewGroup(Context context, AttrSet attrs) {
        super(context, attrs);
        informConfigurationChanged(context.getResourceManager().getConfiguration());  // Necessary?
        setEstimateSizeListener(this);
        setArrangeListener(this);
    }

    @Rotation.Enum
    public int getChildRotation() {
        return mRotation;
    }

    public void setChildRotation(@Rotation.Enum int rotation) {
        if (mRotation != rotation) {
            mRotation = rotation;
            mRotationChanged = true;
            postLayout();
        }
    }

    public boolean isChildRotated() {
        return Rotation.isRotated(mRotation);
    }

    public boolean dispatchTouchEvent(TouchEvent event) {
        mViewTouchPoint[0] = event.getPointerPosition(0).getX();
        mViewTouchPoint[1] = event.getPointerPosition(0).getY();

        mEventTransformMatrix.mapPoints(mChildTouchPoint, mViewTouchPoint);
        event.setScreenOffset(mChildTouchPoint[0], mChildTouchPoint[1]);
//        boolean result = super.dispatchTouchEvent(event);
        event.setScreenOffset(mViewTouchPoint[0], mViewTouchPoint[1]);

        return true;
    }

//    @Override
//    public ComponentParent invalidateChildInParent(int[] location, Rect dirty) {
//        invalidate();
//        return super.invalidateChildInParent(location, dirty);
//    }

    @Override
    public boolean onEstimateSize(int widthMeasureSpec, int heightMeasureSpec) {
//        int horzPadding = getPaddingLeft() + getPaddingRight();
//        int vertPadding = getPaddingTop() + getPaddingBottom();
//
//        int childWidthMSpec, childHeightMSpec;
//        if (isChildRotated()) {
//            // Swap both measure specs, and subtracted paddings.
//            childWidthMSpec = EstimateSpec.getSizeWithMode(
//                    Math.max(EstimateSpec.getSize(heightMeasureSpec) - vertPadding, 0),
//                    EstimateSpec.getMode(heightMeasureSpec));
//            childHeightMSpec = EstimateSpec.getSizeWithMode(
//                    Math.max(EstimateSpec.getSize(widthMeasureSpec) - horzPadding, 0),
//                    EstimateSpec.getMode(widthMeasureSpec));
//        } else {
//            // Subtract the paddings from measure specs.
//            childWidthMSpec = EstimateSpec.getSizeWithMode(
//                    Math.max(EstimateSpec.getSize(widthMeasureSpec) - horzPadding, 0),
//                    EstimateSpec.getMode(widthMeasureSpec));
//            childHeightMSpec = EstimateSpec.getSizeWithMode(
//                    Math.max(EstimateSpec.getSize(heightMeasureSpec) - vertPadding, 0),
//                    EstimateSpec.getMode(heightMeasureSpec));
//        }
//
//        int maxChildWidth = 0, maxChildHeight = 0;
//        int childCount = getChildCount();
//        for (int i = 0; i < childCount; ++i) {
//            Component child = getComponentAt(i);
//            child.estimateSize(childWidthMSpec, childHeightMSpec);
//            maxChildWidth = Math.max(maxChildWidth, child.getWidth());
//            maxChildHeight = Math.max(maxChildHeight, child.getHeight());
//        }
//
//        if (isChildRotated()) {
//            setEstimatedSize(maxChildHeight + horzPadding, maxChildWidth + vertPadding);
//        } else {
//            setEstimatedSize(maxChildWidth + horzPadding, maxChildHeight + vertPadding);
//        }
        return false;
    }

    @Override
    public boolean onArrange(int left, int top, int right, int bottom) {
//        if (mRotationChanged) {
//            int width = right - left;
//            int height = bottom - top;
//
//            switch (Rotation.normalize(mRotation, this)) {
//                default:
//                case Rotation.NONE:
//                    mChildLayoutRect.set(getPaddingLeft(), getPaddingTop(),
//                            width - getPaddingRight(), height - getPaddingBottom());
//                    mEventTransformMatrix.reset();
//                    mDrawTransformMatrix.reset();
//                    break;
//
//                case Rotation.CLOCKWISE:
//                    mChildLayoutRect.set(getPaddingTop(), getPaddingRight(),
//                            height - getPaddingBottom(), width - getPaddingLeft());
//                    mEventTransformMatrix.setRotate(-90);
//                    mEventTransformMatrix.postTranslate(0, width);
//                    mEventTransformMatrix.invert(mDrawTransformMatrix);
//                    break;
//
//                case Rotation.COUNTER_CLOCKWISE:
//                    mChildLayoutRect.set(getPaddingBottom(), getPaddingLeft(),
//                            height - getPaddingTop(), width - getPaddingRight());
//                    mEventTransformMatrix.setRotate(90);
//                    mEventTransformMatrix.postTranslate(height, 0);
//                    mEventTransformMatrix.invert(mDrawTransformMatrix);
//                    break;
//            }
//            mRotationChanged = true;
//        }
//
//        int childCount = getChildCount();
//        for (int i = 0; i < childCount; ++i) {
//            Component child = getComponentAt(i);
//            child.arrange(mChildLayoutRect.left, mChildLayoutRect.top, mChildLayoutRect.right,
//                    mChildLayoutRect.bottom);
//        }
        return false;
    }

    //旋转关键代码，暂时无法替换
//    @Override
//    protected void dispatchDraw(Canvas canvas) {
//        canvas.save();
//        canvas.concat(mDrawTransformMatrix);
//        super.dispatchDraw(canvas);
//        canvas.restore();
//    }


}
